package com.droidathletics.boundary.microcycles;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.droidathletics.R;
import com.droidathletics.boundary.MainMenu;
import com.droidathletics.control.ManageMicrocycle;
import com.droidathletics.entity.Macrocycle;
import com.droidathletics.entity.TypeTraining;

public class CreateMicrocycles extends Activity {

    
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_microcycle);
        
        instance=this;
        
        Bundle b = getIntent().getExtras();
        int position = b.getInt("key");
        
        final Macrocycle editedMacrocycle = (Macrocycle) MainMenu.listMacrocycles.getAdapter().getItem(position);
        
        initializeTrainings();
        
        manageMicrocycle = MainMenu.manageMicrocycle;
        
        weekNumber = (EditText) findViewById(R.id.fieldMicrocycleWeekNumber);
        period = (EditText) findViewById(R.id.fieldMicrocyclePeriod);
        stage = (EditText) findViewById(R.id.fieldMicrocycleStage);
        type = (EditText) findViewById(R.id.fieldMicrocycleType);
        objectives = (EditText) findViewById(R.id.fieldMicrocycleObjectives);
        hoursWeek = (EditText) findViewById(R.id.fieldMicrocycleHoursWeek);
        
        errorData = (TextView) findViewById(R.id.textMicrocyclesError);

        saveButton = (Button) findViewById(R.id.buttonMicrocycleSave);
        
        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String macrocycleName = editedMacrocycle.getName();
            	String errorResult = manageMicrocycle.createMicrocycle(
            			macrocycleName, 
            			weekNumber.getText().toString(), 
            			period.getText().toString(), 
            			stage.getText().toString(), 
            			type.getText().toString(), 
            			objectives.getText().toString(), 
            			hoursWeek.getText().toString(), 
            			trainings);
            	
            	if ( errorResult == "")
            	{
            		finish();
                	Intent intent = new Intent(CreateMicrocycles.this, MainMenu.class);
                	//MainMenu.addDrawable();
                    startActivity(intent);
            	}
            	else
            	{
            		errorData.setText(errorResult);
            	}

            }
        });
        
        spinnerTrainings = (Spinner) findViewById(R.id.spinnerMicroTrainings);
        
        
	}
	
	private static String[] touchedTrainings(){
		Iterator<TypeTraining> iter = CreateMicrocycles.trainings.iterator();
		ArrayList<String> touchedTrainings = new ArrayList<String>();
    	while(iter.hasNext()){
    		TypeTraining tempTraining = iter.next();
    		if(tempTraining.getVolume()!=0){
    			touchedTrainings.add(tempTraining.getName()+" "+tempTraining.getVolume());
    		}
    	}
    	String[] returnedValues = new String[touchedTrainings.size()];
    	for(int i=0;i<returnedValues.length;i++){
    		returnedValues[i]=touchedTrainings.get(i);
    	}
    	return returnedValues;
	}
	
	static public void setAdapterSpinner(){
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(instance,
				android.R.layout.simple_spinner_item, touchedTrainings());
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerTrainings.setAdapter(adapter);
	}
	
	public void initializeTrainings()
	{
	   trainings.add( new TypeTraining("ANALAC-VELOCIDAD"));
	   trainings.add( new TypeTraining("ANLAC-RESIST-VELOC"));
	   trainings.add( new TypeTraining("ANA-AEROBIO-MIXTO"));
	   trainings.add( new TypeTraining("AEROBICO"));
	   trainings.add( new TypeTraining("FUERZA MAXIMA"));
	   trainings.add( new TypeTraining("FUERZA RAPIDA"));
	   trainings.add( new TypeTraining("FUERZA RESISTENCIA"));
	   trainings.add( new TypeTraining("FLEXIBILIDAD"));
	   trainings.add( new TypeTraining("CAP.COORDINATIVAS"));
	   trainings.add( new TypeTraining("TECNICA"));
	   trainings.add( new TypeTraining("TECNICA-TACTICA"));
	   trainings.add( new TypeTraining("P.PSICOLOGICA"));
	   trainings.add( new TypeTraining("P.BIOLOGICA"));
	   trainings.add( new TypeTraining("P. TEORICA"));
	   trainings.add( new TypeTraining("COMPETENCIAS"));
	   trainings.add( new TypeTraining("CONTROLES"));
	   
	}
	@Override
    public void onBackPressed() {
		finish();
        Intent intent = new Intent(CreateMicrocycles.this, MainMenu.class);
        startActivity(intent);
	    return;
    }
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.training_volume_menu, menu);
        return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.volume:
            Intent intent = new Intent(CreateMicrocycles.this, CreateMicrocyclesTrVolume.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	public static String[] trainingValues = new String[] { "ANALAC-VELOCIDAD",
       		"ANLAC-RESIST-VELOC", "ANA-AEROBIO-MIXTO", "AEROBICO", "FUERZA MAXIMA", "FUERZA RAPIDA", 
       		"FUERZA RESISTENCIA", "FLEXIBILIDAD", "CAP.COORDINATIVAS", "TECNICA",
       		"TECNICA-TACTICA", "P.PSICOLOGICA", "P. TEORICA", "COMPETENCIAS", "CONTROLES"};
	private static CreateMicrocycles instance;
	private EditText weekNumber;
	private EditText period;
    private EditText stage;
    private EditText type;
    private EditText objectives;
    private EditText hoursWeek;
    private TextView errorData;
    private Button saveButton;
    private ManageMicrocycle manageMicrocycle;
    private static Spinner spinnerTrainings;
    public static ArrayList<TypeTraining> trainings= new ArrayList<TypeTraining>();
}
